package com.anhtuan.reminder.ultils;

import java.util.List;

import com.anhtuan.reminder.R;
import com.anhtuan.reminder.listelement.BaseListElement;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<BaseListElement> {
	private List<BaseListElement> listElements = null;

	public ListAdapter(Context context, int textViewResourceId,
			List<BaseListElement> listElements) {
		super(context, textViewResourceId, listElements);
		// TODO Auto-generated constructor stub
		this.listElements = listElements;

		for (int i = 0; i < listElements.size(); i++) {
			listElements.get(i).setAdapter(this);
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;

		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.listitem, null);
		}

		BaseListElement baseListElement = listElements.get(position);

		if (listElements != null) {
			view.setOnClickListener(baseListElement.getOnClickListener());
			ImageView icon = (ImageView) view.findViewById(R.id.icon);
			TextView text = (TextView) view.findViewById(R.id.action_text);
			TextView time = (TextView) view
					.findViewById(R.id.action_description);

			if (icon != null) {
				icon.setImageDrawable(baseListElement.getIcon());
			}

			if (text != null) {
				text.setText(baseListElement.getText());
			}

			if (time != null) {
				time.setText(baseListElement.getDescription());
			}
		}

		return view;
	}

	@Override
	public View getDropDownView(int position, View convertView,
			ViewGroup parent) {
		View view = convertView;

		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.listitem, null);
		}

		BaseListElement baseListElement = listElements.get(position);

		if (listElements != null) {
			ImageView icon = (ImageView) view.findViewById(R.id.icon);
			TextView text = (TextView) view.findViewById(R.id.action_text);
			TextView time = (TextView) view
					.findViewById(R.id.action_description);

			if (icon != null) {
				icon.setImageDrawable(baseListElement.getIcon());
			}

			if (text != null) {
				text.setText(baseListElement.getText());
			}

			if (time != null) {
				time.setText(baseListElement.getDescription());
			}
		}

		return view;
	}
}
